<?php
namespace app\common\model;

use think\model\concern\SoftDelete;

class CourseSignup extends Base
{
//
//    // 软删除
//    use SoftDelete;
//    protected $deleteTime = 'is_del';
//    protected $defaultSoftDelete = 1;

    // 设置当前模型对应的完整数据表名称
    protected $table = 'ss_course_signup';
    // 主键ID
    protected $pk = 'sup_id';

    /**
     * @param array $where  // 查询的条件
     * @param string $item  // 查询的参数
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function getOne($where=[1], $item='*')
    {
        $rs = $this
            ->field($item)
            ->where($where)
            ->find();

        return $rs;
    }

    function get_list($field='*',$cond=null, $orderby='sort asc')
    {
        $where = null;

        if(!empty($cond['parent_id'])){
            $where[] = ['parent_id','=', $cond['parent_id']];
        }
        if(!empty($cond['shop_id'])){
            $where[] = ['shop_id','=', $cond['shop_id']];
        }
        if(!empty($cond['is_del'])){
            $where[] = ['is_del','=', $cond['is_del']];
        }
        if(!empty($cond['ac_name'])){
            $where[] = ['ac_name','LIKE', "%{$cond['ac_name']}%"];
        }

        $rs = $this
            ->field($field)
            ->where($where)
            ->order($orderby)
            ->select();
        return $rs;
    }

    // 获取分页列表
    function get_list_page($field,$cond,$limit, $orderby='sort asc')
    {
        $where = null;

        if(!empty($cond['course_name'])){
            $where[] = ['course_name','LIKE', "%{$cond['course_name']}%"];
        }

        $rs = $this->field($field)
            ->where($where)
            ->order($orderby)
            ->paginate($limit);

        return $rs;
    }

    //获取树形数据
    function get_tree($field,$cond, $orderby='sort asc')
    {
        $where = null;

        if(!empty($cond['is_del'])){
            $where[] = ['is_del','=', $cond['is_del']];
        }
        if(!empty($cond['shop_id'])){
            $where[] = ['shop_id','=', $cond['shop_id']];
        }
        if(!empty($cond['ac_name'])){
            $where[] = ['ac_name','LIKE', "%{$cond['ac_name']}%"];
        }
        $rs = $this
            ->field($field)
            ->where($where)
            ->order($orderby)
            ->select()
            ->toArray();
        if(!$rs){
            return [];
        }

        foreach ($rs as $key => $value){
            if($value['parent_id'] === 0){
                $fdata[$value['ac_id']] = $value;
                $fdata[$value['ac_id']]['children'] = [];
            }else{
                $fdata[$value['parent_id']]['children'][] = $value;
            }
        }

        return array_values($fdata);
    }

    //新增、编辑(后台操作)
    function edit($params, $id=0){
        if($id>0){
            // 更新条件
            $where = ["{$this->pk}" => $id];
//            $params['update_time'] = time();
            $rs = $this->allowField(true)->save($params, $where);
        }else{
            // 新增数据
//            $params['add_time'] = time();
            $this->allowField(true)->save($params);
            $rs = $this->{$this->pk};
        }
        if ($rs === false) {
            $this->setError(-1, '操作失败');
            return false;
        }
        return $rs;
    }

}